var offset=0;
var limit=20;
var pagingLimit=20;//显示分页时的条数
var currentTotalElements=0;//当前显示总条数
var pageLoading=false;//是否能滚动加载
var hasShowPages=false;//是否已显示分页
var firstLoadPages=true;//分页初始化时 不自动加载
//筛选条件,
var search_carNumber=null;
var search_employName=null;
var search_companyName=null;
var search_carType=null;
var search_status=[];
var shopId;
var isGrounding;
var searchParam;

$(document).ready(function(){

	$("#searchButton").click(function(){
		searchParam = $("#searchParam").val();
		shopId = $("#shopId option:selected").val();
		isGrounding = $("#isGrounding").val();
		initdata(false);
	});
	
//	$(document).on("keydown","body",function(){
//		if(window.event.keyCode == 13){
//			$("#searchButton").click();
//		}
//	});
	
//    $(".isGrounding").click(function(){
//          thisButton = $(this);
//          var confirm = $.scojs_confirm({
//            content: "你真的要上架该车辆吗？",
//            //action:deleteCar
//          });
//          confirm.show();
//    });
    
//    $(".updatethis").click(function(){
//        thisButton = $(this);
//        var confirm = $.scojs_confirm({
//        });
//        confirm.show();
//    });
    initdata(false);
});

function initdata(isappend){
	$.ajax({
        url : ctx+'/car/carsData',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"shopId":shopId,
        	"isGrounding":isGrounding,
        	"searchParam":searchParam,
        	"state":0,
        	"pager.offset":offset,
        	"limit":limit,
        },
        success : function(result){
        	/*var newcontent = '';
        	var totalElements=result.totalElements;
        	if(totalElements>0){
        		$(result.content).each(function(index,data){
        			var statusColor="";
        			var mainButton="";
        			var selectButton="";
            			statusColor="btn-primary";
        				mainButton=
        					"<a href="+ctx+"'/car/edit/"+data.id+"' data-value='"+data.id+";' class='btn btn-primary btn-xs'>编辑</a>";
        				 
        				mainButton+="<a href='#' data-value='"+data.id+"' class='btn btn-primary btn-xs isGrounding'>上架</a>";
            		var allButton="<div class='btn-group'>"+
            							mainButton+
									"</div>";
            	
            	var status = "";	
            	if(data.status==0){
            		status="<span class='label label-info'>空闲</span>";
            	}else if(data.status==1){
            		status="<span class='label label-warning'>忙碌</span>";
            	}else if(data.status==2){
            		status="<span class='label label-danger'>离线 </span>";
            	}
            	var limitLine="";
            	if(data.limitLine==1){
            		limitLine="星期一";
            	}else if(data.limitLine==2){
            		limitLine="星期二";
            	}else if(data.limitLine==3){
            		limitLine="星期三";
            	}else if(data.limitLine==4){
            		limitLine="星期四";
            	}else if(data.limitLine==5){
            		limitLine="星期五";
            	}else{
            		limitLine="不限行";
            	}
            		
            		
	    newcontent+="<tr class='grid-data tr-hui'  >"+
	                    "<td>"+"--"+"</td>"+
	                    "<td>"+"--"+"</td>"+
	                    "<td>"+"--"+"</td>"+
						"<td>"+data.carNumber+"</td>"+
						"<td>"+"--"+"</td>"+
						"<td>"+"--"+"</td>"+
						"<td>"+"--"+"</td>"+
						"<td>"+"--"+"</td>"+
						"<td>"+"--"+"</td>"+
						"<td>"+"--"+"</td>"+
						"<td>"+"--"+"</td>"+
						"<td>"+"--"+"</td>"+
						"<td>"+"--"+"</td>"+
						"<td>"+"--"+"</td>"+
						"<td>"+status+"</td>"+
						"<td>"+allButton+"</td>"+
					"</tr>";
        		});
        	}
        	if(!isappend){
        		$("#rentcar_tbody .grid-data").remove();
        	}
        	$("#rentcar_tbody").append(newcontent);
        	$("#rentcar_tbody .grid-data").each(function(index,element){
        		$(element).fadeIn(200+(index*35));
        	});
        	if(!hasShowPages){
        		if(!isappend){
            		currentTotalElements=result.content.length;
            		pageLoading=true;
            	}else{
            		if(currentTotalElements<totalElements){
            			currentTotalElements+=result.content.length;
            			pageLoading=true;
            		}else{
            			pageLoading=false;
            		}
            	}
        	}
        	
        	if(hasShowPages){
    			firstLoadPages=true;
        		$(".pageshow li a").html("共"+totalElements+" 条数据");
        		$(".pagedata").paging(totalElements, {
        			format: '[< ncnnn >]',
        			perpage: pagingLimit,
        			page:(offset/limit)+1,
        			onSelect: function (page) {
        				limit=pagingLimit;
        				offset=(page-1)*limit;
        				if(!firstLoadPages){
        					initdata(false);
        				}
        				firstLoadPages=false;
        		    	return false;
        			},
        			onFormat: function (type) {
        				switch (type) {
        				case 'block': // n and c
        					if (!this.active)
        						return '';
        					else if (this.value != this.page)
        						return '<li><a href="#">' + this.value + '</a></li>';
        					return '<li class="active"><a href="#">' + this.value + '</a></li>';
        				case 'next': // >
        					return '<li><a href="#">&rsaquo;</a><li>';
        				case 'prev': // <
        						return '<li><a href="#">&lsaquo;</a><li>';
        				case 'first': // [
        						return '<li><a href="#">&laquo;</a><li>';
        				case 'last': // ]
        						return '<li><a href="#">&raquo;</a></li>';
        				}
        			}
        		});
        	}else{
            		pageLoading=false;
            		hasShowPages=true;
            		$(".pageshow").show();
            		$(".pagedata").show();
            		$(".pageshow li a").html("共"+totalElements+" 条数据");
            		$(".pagedata").paging(totalElements, {
            			format: '[< ncnnn >]',
            			perpage: pagingLimit,
            			onSelect: function (page) {
            				limit=pagingLimit;
            				offset=(page-1)*limit;
            				if(!firstLoadPages){
            					initdata(false);
            				}
            				firstLoadPages=false;
            		    	return false;
            			},
            			onFormat: function (type) {
            				switch (type) {
            				case 'block': // n and c
            					if (!this.active)
            						return '';
            					else if (this.value != this.page)
            						return '<li><a href="#">' + this.value + '</a></li>';
            					return '<li class="active"><a href="#">' + this.value + '</a></li>';
            				case 'next': // >
            					return '<li><a href="#">&rsaquo;</a><li>';
            				case 'prev': // <
            						return '<li><a href="#">&lsaquo;</a><li>';
            				case 'first': // [
            						return '<li><a href="#">&laquo;</a><li>';
            				case 'last': // ]
            						return '<li><a href="#">&raquo;</a></li>';
            				}
            			}
            		});
        	}*/
        }
	});
}


